class FamiliaController < ApplicationController

  def index
    list
    
    render :action => "list"
  end
  
  def list
    @familias = Familia.paginate :page => params[:page], :per_page => DEFAULT_PER_PAGE, :order => 'nombre'
  end

  def edit
    @familia = Familia.find(params[:id])
    populate_collections
  end

  def new
    @familia = Familia.new
    populate_collections
  end
  
  def populate_collections
    @categorias = Categoria.find(:all, :order => 'nombre')
    @empresas = Empresa.find(:all, :order => 'nombre')
  end
  
  def update
    @familia = Familia.find(params[:id])
    
    if @familia.update_attributes(params[:familia])
      flash[:notice] = "Se guardo correctamente."
      
      redirect_to :action => "index"
    else
      populate_collections
      
      render :action => "edit", :id => @familia
    end
  end
  
  def create
    @familia = Familia.new(params[:familia])
    
    if @familia.save
      flash[:notice] = "Se guardo correctamente."
      
      redirect_to :action => "index"
    else
      populate_collections
      
      render :action => "new"
    end
  end
end
